package com.mlz.mlzmall.ui.activity.frag.main;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.ListView;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.ui.activity.act.main.FreeTrialActivity;
import com.mlz.mlzmall.ui.activity.act.main.GoogDetailsActivity;
import com.mlz.mlzmall.ui.dialog.ReuestDialog;
import com.mlz.mlzmall.ui.utils.util.LogUtils;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;

/**
 * Created by Pm on 2016/5/18.
 */
public class SpecialFragment extends Fragment {
    private ListView listView;
    private Context context;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity();
        View rootView = inflater.inflate(R.layout.fragment_special, container, false);//关联布局文件
        listView = (ListView) rootView.findViewById(R.id.lv_manufacturer_activity);
       // ViewUtils.inject(this, rootView);
        listView.setAdapter(new MyAdapter());
        return  rootView;
    }
    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 1 ;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(context).inflate(R.layout.topic_listview_item, null);
            ViewHolder vh = new ViewHolder();
            vh.wv_topic_activity = (WebView) convertView.findViewById(R.id.wv_topic_activity);


            if (null != convertView) {

                String url ="http://m.mlzmall.com/Home/Manufacturer?activity=" + SharedUtils.get(getActivity(),"manufacturerId",0) + "&app=1";
                LogUtils.d(url);
                //此方法可以在webview中打开链接而不会跳转到外部浏览器
                vh.wv_topic_activity.getSettings().setJavaScriptEnabled(true);//webview适配器
                ReuestDialog.showProgressDialog(getActivity());
                vh.wv_topic_activity.loadUrl(url);
                ReuestDialog.dissmissProgressDialog();
                vh.wv_topic_activity.setWebViewClient(new HelloWebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {

                        // 跳转产品详情 url.startsWith("Product",26)
                        if (url.startsWith("http://m.mlzmall.com/Home/Product")) {
                            int start = url.lastIndexOf("=");
                            int productIndex = start + "=".length();
                            Log.i("info", "url-product-->" + url);
                            Intent intentToGoods = new Intent(getActivity(), GoogDetailsActivity.class);
                            intentToGoods.putExtra("productId", Integer.valueOf(url.substring(productIndex)));
                            intentToGoods.putExtra("SpecialFragment","SpecialFragment");
                            context.startActivity(intentToGoods);
                        //    StringTools.topActivity = 199;
                         //   ((Activity) context).finishFromChild(((Activity) context).getParent());
                        }

//                        // 跳转免费试用,没写
//                        if (url.startsWith("http://m.mlzmall.com/Home/Share")) {
//                            Intent intentToFree = new Intent(context.getApplicationContext(), FreeTrialActivity.class);
//                            intentToFree.putExtra("promotionIdUrl", "http://test.m.mlzmall.com/Home/Share/appAward?promotion_id="+SharedUtils.get(getActivity(),"promotionId",0));
//                            context.startActivity(intentToFree);
//                        //    ((Activity) context).finishFromChild(((Activity) context).getParent());
//                        }
                        //跳转到组合套装，没写
 //                       if (url.startsWith("http://m.mlzmall.com/Home/setmeal")) {
//                            Intent intentToSetmeal = new Intent(context.getApplicationContext(), MainActivity.class);
//                            intentToSetmeal.putExtra("setMealUrl", url);
//                            StringTools.topActivity=199;
//                            context.startActivity(intentToSetmeal);
//                            Log.i("info", "url-setMeal-->" + url);
                        //    ((Activity) context).finishFromChild(((Activity) context).getParent());
//
 //                       }


                  //      return super.shouldOverrideUrlLoading(view, url);
                        return true;

                    }
                });
            }
            return convertView;
        }
        class ViewHolder {
            WebView wv_topic_activity;
        }

        private class HelloWebViewClient extends WebViewClient {

            @Override
            public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
                return super.shouldOverrideKeyEvent(view, event);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }
    }
}
